﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace KidsProtector.DataAccess
{
    public class Site
    {
        public bool CreateSite(T_Site site)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                model.AddToT_Site(site);
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public int GetMoy(long idSite)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Site site = model.T_Site.Where(x => x.Id == idSite).FirstOrDefault();
                return site.Moyenne;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public T_Site GetSite(long idSite)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Site site = model.T_Site.Where(x => x.Id == idSite).FirstOrDefault();
                return site;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public T_Site GetSite(string site)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Site s = model.T_Site.Where(x => x.Adress == site).FirstOrDefault();
                return s;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public bool UpdateMoy(long idSite)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                List<T_Rate> l = model.T_Rate.Where(x => x.IdSite == idSite).ToList();
                int sum = 0;
                if (l.Count != 0)
                {
                    foreach (T_Rate r in l)
                        sum += r.note;
                    ModifyMoy(idSite, sum / l.Count);
                }
                Serialize();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool ModifyMoy(long idSite, int moy)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Site site = model.T_Site.Where(x => x.Id == idSite).FirstOrDefault();
                site.Moyenne = moy;
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        public List<T_Site> GetList()
        {
            List<T_Site> list;
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                list = model.T_Site.ToList();
            }
            catch (Exception)
            {
                list = new List<T_Site>();
            }

            return list;
        }

        public void Serialize()
        {
            BusinessManagement.Rate rateBM = new BusinessManagement.Rate();
            //Création du XML avec le noeud root
            XDocument doc = XDocument.Parse("<grades></grades>");

            List<T_Site> list = GetList();
            if (list.Count != 0)
            {
                //ajout d'un attribut
                doc.Root.Add(new XAttribute("nb", list.Count));
            }

            foreach (T_Site site in list)
            {
                //construction de chaque noeud
                XElement elt = new XElement("grade",
                    new XAttribute("nb", rateBM.GetCount(site.Id)),
                    new XElement("site", site.Adress),
                    new XElement("average", site.Moyenne));
                //ajout du noeud au reste de l'arbre
                doc.Root.Add(elt);
            }

            string path = HttpContext.Current.Server.MapPath("~/Download");
            try
            {
                doc.Save(path + "/site.xml");
            }
            catch (Exception)
            {
            }
        }
    }
}

